<?php

use swap\lazy_checker;
use swap\check_failed;
use swap\visitor;
use swap\captcha;
use swap\config;

class site_controller extends \swap\controller {
	
	public static $layout = 'my';
	
	public static function index_action() {
		self::forward_to('my-site/login');
	}
	
	public static function login_action() {
		if (visitor::has_role('user')) {
			self::redirect_to('my-user/index');
		}
		self::show_page();
	}
	
	public static function do_login_action() {
		// 拦截
		self::method('post');
		
		try {
			// 校验
			$checker = new lazy_checker(p());
			$checker->check_model_rules('user', 'login');
			$post = $checker->get_all();
			extract($post);
			// 二次校验
			$used = visitor::get_role_var('guest', 'captcha_used');
			if ($used) {
				$checker->failed('validate', '验证码错误');
			}
			$captcha = visitor::get_role_var('guest', 'captcha');
			if ($captcha !== $validate) {
				$checker->failed('validate', '验证码错误');
			}
			$user = user_model::get_one(array('email' => $email));
			if ($user === null) {
				$reg_url = url('my-site/reg');
				$checker->failed('email', '该E-mail地址尚未注册' . "<br /><a href='{$reg_url}'>立即注册</a>");
			}
			if (!$user->is_valid_password($password, $user->salt)) {
				$checker->failed('password', '密码错误!');
			}
			// 设置
			visitor::set_role('user', $user->id, 0, array('email' => $email));
			// 发送
			self::json_result(true, '登陆成功', 1, url('my-mine/index'));
		} catch (check_failed $e) {
			$message = $e->get_reasons();
			self::json_result(false, current($message), 0, key($message));
		}
	}
	
	public static function reg_action() {
		self::show_page();
	}
	
	public static function do_reg_action() {
		// 拦截
		self::method('post');
		
		try {
			// 校验
			$checker = new lazy_checker(p());
			if (!p_has('accept')) {
				$checker->failed('accept', '请接受智联招聘的用户协议和隐私政策');
			}
			$checker->check_model_rules('user', 'reg');
			// 执行
			$salt = random_sha1();
			$password = user_model::generate_password($checker->get('password'), $salt);
			$add = array(
				'email' => $checker->get('email'),
				'password' => $password,
				'salt' => $salt,
			);
			$user_id = user_model::add($add);
			// user session
			visitor::set_role('user', $user_id, 0, array('email' => $checker->get('email')));
			// 发送
			self::json_result(true, '注册成功', 1, url('my-mine/index'));
		} catch (check_failed $e) {
			$message = $e->get_reasons();
			self::json_result(false, current($message), 0, key($message));
		}
	}
	
	public static function guest_captcha_action() {
		$text = '杨志兵啊';
		if (visitor::has_role('guest')) {
			visitor::set_role_var('guest', 'captcha', $text);
		} else {
			visitor::set_role('guest', 0, 0, array('captcha' => $text));
		}
		captcha::set_text($text);
		captcha::set_noise_level(config::get('technique.captcha.noise_level', 1));
		captcha::set_font_file(swap\share_dir . '/font/simfang.ttf');
		captcha::set_size(150, 60);
		visitor::no_cache();
		visitor::set_role_var('guest', 'captcha_used', false);
		self::send(captcha::generate('zh'), 'image/png');
	}
	
}